#! /usr/bin/python
from optparse import OptionParser
import sys,os, glob,shutil

if __name__ == "__main__":

#  parser = OptionParser() 
#  parser.add_option ("-d","--dry", 
#                     help="Dry run", 
#                     dest="dry", 
#                     action="store_true", 
#                     default=False) 
#  options,args = parser.parse_args() 
#  dry          = options.dry

  trashbin = "~/scripts/trash/"
  extensions = [".sh",".py",".cxx",".C",".h"]

  dry = False
  verbose = False
  cmd ="/bin/rm"
  filesg = []
  for arg in sys.argv[1:]:
    arg = arg.replace("(","\(").replace(")","\)")
    if arg.find("*") > -1:
      if arg == "*":
        input = raw_input("You are trying to remove everything, are you sure? y/[n]: ")
        if input != "y":
          sys.exit(1)
      filesg.append(glob.glob(arg))
    elif not arg.startswith("-"):
      filesg.append(glob.glob(arg))
    elif arg == "-d":
      dry = True
      continue
    elif arg == "-v":
      verbose = True
    cmd += " "+arg

  files = []
  for list in filesg:
    for file in list:
      if file not in files:
        files.append(file)
        for ext in extensions:
          if file.endswith(ext):
            try: #to be able to remove files in the trashbin
              shutil.copy(file,os.path.expanduser(trashbin))
            except:
              pass
            break

  if dry or verbose:
    print files
  if not dry:
    if verbose:
      input = raw_input("Are you sure that you want to remove these files? y/[n]: ")
      if input != "y":
        sys.exit(1)
    stdout = os.popen(cmd).read()
    if stdout != "":
      print stdout
      
